{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算均值和方差的几种方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean:0.006087\n",
      "variance:0.253493\n",
      "standard_deviation:0.503481\n"
     ]
    }
   ],
   "source": [
    "## 1.使用python函数计算\n",
    "\n",
    "# 数据\n",
    "data = [0.16, -0.67, -0.21, 0.54, 0.22, -0.15, -0.63, 0.03, 0.88, -0.04, 0.20, 0.52, -1.03, 0.11, 0.49, -0.47,0.35, 0.80, -0.33, -0.24, -0.13, -0.82, 0.56]\n",
    "# 均值\n",
    "mean = sum(data) / len(data)\n",
    "print(\"mean:%f\" % mean)\n",
    "\n",
    "square_data = []\n",
    "for i in data:\n",
    "    # 计算平方\n",
    "    square = pow((i - mean), 2)\n",
    "    # print(\"square:%f\" % square)\n",
    "    square_data.append(square)\n",
    "pass\n",
    "\n",
    "# 方差\n",
    "variance = sum(square_data) / len(square_data)\n",
    "print(\"variance:%f\" % variance)\n",
    "\n",
    "# 标准差等于方差开平方根\n",
    "import math\n",
    "standard_deviation = math.sqrt(variance)\n",
    "print(\"standard_deviation:%f\" % standard_deviation)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "variance:0.253493\n",
      "standard_deviation:0.503481\n",
      "CPU times: user 740 µs, sys: 385 µs, total: 1.12 ms\n",
      "Wall time: 786 µs\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "## 2.使用numpy框架计算\n",
    "import numpy as np\n",
    "# 数据\n",
    "data = [0.16, -0.67, -0.21, 0.54, 0.22, -0.15, -0.63, 0.03, 0.88, -0.04, 0.20, 0.52, -1.03, 0.11, 0.49, -0.47,0.35, 0.80, -0.33, -0.24, -0.13, -0.82, 0.56]\n",
    "# 计算方差\n",
    "variance = np.var(data)\n",
    "print(\"variance:%f\" % variance)\n",
    "# 计算标准差\n",
    "# %time\n",
    "standard_deviation = np.std(data)\n",
    "print(\"standard_deviation:%f\" % standard_deviation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cov_x_y:0.4385294896030246\n",
      "cov_xy:[[0.25349338 0.43852949]\n",
      " [0.43852949 2.46846881]]\n"
     ]
    }
   ],
   "source": [
    "## 3.计算两个变量的协方差\n",
    "x = [0.16, -0.67, -0.21, 0.54, 0.22, -0.15, -0.63, 0.03, 0.88, -0.04, 0.20, 0.52, -1.03, 0.11, 0.49,     -0.47, 0.35, 0.80, -0.33, -0.24, -0.13, -0.82, 0.56]\n",
    "\n",
    "y = [0.07, -0.55, -0.04, 3.11, 0.28, -0.50, 1.10, 1.97, -0.31, -0.55, 2.06, -0.24, -1.44, 1.56, 3.69,     0.53, 2.30, 1.09, -2.63, 0.29, 1.30, -1.54, 3.19]\n",
    "# 两个列表数据长度一样\n",
    "if len(x) == len(y):\n",
    "    e_x = sum(x) / len(x)\n",
    "    e_y = sum(y) / len(y)\n",
    "\n",
    "    xy = []\n",
    "    for i in range(0, len(x)):\n",
    "        xy.append((x[i] - e_x) * (y[i] - e_y))\n",
    "        pass\n",
    "\n",
    "    cov_x_y = sum(xy) / len(xy)\n",
    "    print(\"cov_x_y:%s\" % cov_x_y)\n",
    "\n",
    "    import numpy as np\n",
    "\n",
    "    cov_xy = np.cov(m=x, y=y, ddof=0)\n",
    "    print(\"cov_xy:%s\" % cov_xy)\n",
    "\n",
    "else:\n",
    "    print(\"x and y length is not equal\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
